home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 04 - 1988 / 04.11 Nov 88 / IAC / Editor Stuff / Edit_rtns.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-09-18  |  9.7 KB  |  367 lines  |  [TEXT/MPS ]

  1. /****
  2.  *
  3.  *    These are the routines needed for the actual text-editing functions. They
  4.  *    are arranged alphabetically for ease of finding them.
  5.  *
  6.  ***/
  7.  
  8. # include    <types.h>
  9. # include    <errors.h>
  10. # include    <memory.h>
  11. # include    <packages.h>
  12. # include    <quickdraw.h>
  13. # include    <toolutils.h>
  14. # include    <fonts.h>
  15. # include    <windows.h>
  16. # include    <dialogs.h>
  17. # include    <menus.h>
  18. # include    <textedit.h>
  19. # include    <string.h>
  20. # include    <files.h>
  21. # include    <resources.h>
  22.  
  23. # include    <iac.h>
  24. # define PUBLIC extern
  25. # include    <Editor.h>
  26.  
  27.  
  28.  /**
  29.  * Routine: do_clear
  30.  *
  31.  *        This is the routine that carries out the "clear" editing function. It
  32.  *    uses the normal TE routine to manipulate the text. In addition, it checks
  33.  *    the source extents to see if any of them are affected by it, and if so,
  34.  *    it will post the changed extent to the IAC driver for everybody else's
  35.  *    use.
  36.  */
  37.  
  38. # define __SEG__ Main
  39. void    do_clear()
  40.  
  41. {
  42. short            this_ed;            /* edition (updated) */
  43. short            hit_ext;            /* extent affect by this call */
  44. short            i;                    /* scratch */
  45. short            strt, endd;            /* range in TE record */
  46. win_dataH        the_data_H;            /* data associated with a window */
  47. extentH            the_extH;            /* handle to extent block */
  48. extentP            extP;
  49. TEHandle        TextH;                /* The TextEdit handle */
  50. Handle            ext_data;            /* data for extent */
  51.  
  52. short            iac_code = noErr;    /* result from IAC call */
  53. Boolean            in_extent = false;
  54. Boolean            ext_killed = false;
  55.  
  56. extern    Boolean    chk_extent();        /* sets "current extent" if found */
  57. extern    void    kill_extent();
  58.  
  59.     /* get necessary handles, etc. set up */
  60.      the_data_H = (win_dataH) GetWRefCon (myWindow);
  61.     TextH =         (**the_data_H).wind_TEH;
  62.     the_extH =     (**the_data_H).the_extents;
  63.  
  64.     /* Check extents before actually cutting. Remember which one and we'll
  65.      * update the edition after doing the delete...
  66.      */
  67.     in_extent = chk_extent(TextH, the_extH, (**the_data_H).ext_cnt);
  68.  
  69.     TEDelete (TextH);
  70.     (**the_data_H).dirty = true;
  71.  
  72.     /* if needed, write revised extent to IAC driver */
  73.     if (in_extent)        /* this delete affected the extent */
  74.     {
  75.         /* adjust range for extent */
  76.  
  77.         /* If the entire extent was removed, tell IAC driver */
  78.         if (ext_killed)
  79.         {
  80.             iac_code = iac_remove_dependency((**the_data_H).doc_ID,
  81.                                               (**the_data_H).the_slot,
  82.                                               curr_ext.hat_check);
  83.             kill_extent();    /* always removes current extent */
  84.         }
  85.         else    /* write to driver */
  86.         {
  87.             this_ed = curr_ext.ed_level;
  88.             /* data setup here */
  89.             iac_code = iac_write_data((**the_data_H).doc_ID, 
  90.                                       curr_ext.hat_check, 
  91.                                       &this_ed,
  92.                                       1,
  93.                                       ext_data);
  94.         /* now adjust extent */
  95.         }
  96.     }
  97. }
  98.  
  99.  
  100.  /**
  101.  * Routine: do_copy
  102.  *
  103.  *        This is the routine that carries out the "copy" editing function. It
  104.  *    uses the normal TE routine to manipulate the text. 
  105.  */
  106.  
  107. # define __SEG__ Main
  108. void    do_copy()
  109.  
  110. {
  111. short            this_ed;            /* edition (updated) */
  112. short            i;                    /* scratch */
  113. short            strt, endd;            /* range in TE record */
  114. win_dataH        the_data_H;            /* data associated with a window */
  115. extentH            the_extH;            /* hand to extent block */
  116. extentP            extP;
  117. TEHandle        TextH;                /* The TextEdit handle */
  118. Handle            ext_data;            /* data for extent */
  119.  
  120. short            iac_code = noErr;    /* result from IAC call */
  121. Boolean            in_extent = false;
  122.  
  123. extern    Boolean    chk_extent();        /* sets "current extent" if found */
  124.  
  125.     /* get necessary handles, etc. set up */
  126.      the_data_H = (win_dataH) GetWRefCon (myWindow);
  127.     TextH =         (**the_data_H).wind_TEH;
  128.     the_extH =     (**the_data_H).the_extents;
  129.     in_extent =     chk_extent(TextH, the_extH, (**the_data_H).ext_cnt);
  130.  
  131.     TECopy (TextH);
  132. }
  133.  
  134.  
  135.  /**
  136.  * Routine: do_cut
  137.  *
  138.  *        This is the routine that carries out the "cut" editing function. It
  139.  *    uses the normal TE routine to manipulate the text. In addition, it checks
  140.  *    the source extents to see if any of them are affected by it, and if so,
  141.  *    it will post the changed extent to the IAC driver for everybody else's
  142.  *    use.
  143.  */
  144.  
  145. # define __SEG__ Main
  146. void    do_cut()
  147.  
  148. {
  149. short            this_ed;            /* edition (updated) */
  150. short            hit_ext;            /* extent affect by this */
  151. short            i;                    /* scratch */
  152. short            strt, endd;            /* range in TE record */
  153. win_dataH        the_data_H;            /* data associated with a window */
  154. extentH            the_extH;            /* hand to extent block */
  155. extentP            extP;
  156. TEHandle        TextH;                /* The TextEdit handle */
  157. Handle            ext_data;            /* data for extent */
  158. OSErr            an_err;
  159.  
  160. short            iac_code = noErr;    /* result from IAC call */
  161. Boolean            in_extent = false;
  162. Boolean            ext_killed = false;
  163.  
  164. extern    Boolean    chk_extent();        /* sets "current extent" if found */
  165. extern    void    kill_extent();
  166.  
  167.     /* get necessary handles, etc. set up */
  168.      the_data_H = (win_dataH) GetWRefCon (myWindow);
  169.     TextH =         (**the_data_H).wind_TEH;
  170.     the_extH =     (**the_data_H).the_extents;
  171.  
  172.     /* Check extents before actually cutting. Remember which one and we'll
  173.      * update the edition after doing the cut...
  174.      */
  175.     in_extent = chk_extent(TextH, the_extH, (**the_data_H).ext_cnt);
  176.  
  177.     TECut (TextH);
  178.     (**the_data_H).dirty = true;
  179.  
  180.     /* if needed, write revised extent to IAC driver */
  181.     if (in_extent)        /* this cut affected the extent */
  182.     {
  183.         /* adjust range for extent */
  184.  
  185.         /* If the entire extent was removed, tell IAC driver */
  186.         if (ext_killed)
  187.         {
  188.             iac_code = iac_remove_dependency((**the_data_H).doc_ID,
  189.                                               (**the_data_H).the_slot,
  190.                                               curr_ext.hat_check);
  191.             kill_extent();    /* always removes current extent */
  192.         }
  193.         else    /* write to driver */
  194.         {
  195.             HLock((Handle) TextH);
  196.             /* data setup here */
  197.             an_err = PtrToHand (&(**TextH).selStart,
  198.                                 &ext_data,
  199.                                 (long) curr_ext.ext_end - curr_ext.ext_strt + 1);
  200.             HUnlock((Handle) TextH);
  201.             this_ed = curr_ext.ed_level;
  202.             iac_code = iac_write_data((**the_data_H).doc_ID, 
  203.                                       curr_ext.hat_check, 
  204.                                       &this_ed,
  205.                                       1,
  206.                                       ext_data);
  207.         } /* end else */
  208.     } /* end in_extent */
  209. }
  210.  
  211.  
  212. /**
  213.  * Routine: do_key
  214.  *
  215.  *        This is the routine that handles normal typing. It does NOT notify
  216.  *    the IAC driver after every keystroke.
  217.  */
  218.  
  219. # define __SEG__ Main
  220. void    do_key(msg)
  221.     long        msg;        /* event record message field */    
  222.  
  223. {
  224. win_dataH        the_data_H;            /* data associated with a window */
  225. extentH            the_extH;            /* hand to extent block */
  226. TEHandle        TextH;                /* The TextEdit handle */
  227. char            the_ch;
  228. short            e_cnt;
  229.  
  230. Boolean            in_extent = false;
  231.  
  232. extern    Boolean    chk_extent();
  233.  
  234. #define charCodeMask 0x000000FF
  235. /*    copied from event.h to conserve space */
  236.  
  237.     /* get necessary handles, etc. set up */
  238.      the_data_H = (win_dataH) GetWRefCon (myWindow);
  239.     TextH =         (**the_data_H).wind_TEH;
  240.     the_extH =     (**the_data_H).the_extents;
  241.     the_ch =     (char) (msg & charCodeMask);
  242.     e_cnt =         (**the_data_H).ext_cnt;
  243.     
  244.     in_extent = chk_extent(TextH, the_extH, e_cnt);
  245.     TEKey(the_ch, TextH);
  246.     (**the_data_H).dirty = true;
  247.  
  248.     /* now adjust extent if necessary */
  249.     if (in_extent)
  250.     {
  251.         if (the_ch==BS)        /* backspace - shrink extent */
  252.         {
  253.             curr_ext.ext_end -= 1;
  254.         }
  255.         else                /* insertion - expand extent */
  256.         {
  257.             curr_ext.ext_end += 1;
  258.         }
  259.     }
  260. }
  261.  
  262.  
  263. /**
  264.  * Routine: do_paste
  265.  *
  266.  *        This is the routine that carries out the "paste" editing function. It
  267.  *    uses the normal TE routine to manipulate the text. In addition, it checks
  268.  *    the source extents to see if any of them are affected by it, and if so,
  269.  *    it will post the changed extent to the IAC driver for everybody else's
  270.  *    use.
  271.  */
  272.  
  273. # define __SEG__ Main
  274. void    do_paste()
  275. {
  276. short            this_ed;            /* edition (updated) */
  277. short            hit_ext;            /* extent affect by this */
  278. short            i;                    /* scratch */
  279. short            delta;                /* change in extent size */
  280. short            strt, endd;            /* range in TE record */
  281. win_dataH        the_data_H;            /* data associated with a window */
  282. extentH            the_extH;            /* hand to extent block */
  283. TEHandle        TextH;                /* The TextEdit handle */
  284. Handle            ext_data;            /* data for extent */
  285.  
  286. short            iac_code = noErr;    /* result from IAC call */
  287. Boolean            in_extent = false;
  288.  
  289. extern    Boolean    chk_extent();        /* sets "current extent" if found */
  290.  
  291.     /* get necessary handles, etc. set up */
  292.      the_data_H = (win_dataH) GetWRefCon (myWindow);
  293.     TextH =         (**the_data_H).wind_TEH;
  294.     the_extH =     (**the_data_H).the_extents;
  295.  
  296.     /* Check extents before actually cutting. Remember which one and we'll
  297.      * update the edition after doing the paste...
  298.      */
  299.     in_extent = chk_extent(TextH, the_extH, (**the_data_H).ext_cnt);
  300.  
  301.     TEPaste (TextH);
  302.     (**the_data_H).dirty = true;
  303.  
  304.     /* if needed, write revised extent to IAC driver */
  305.     if (in_extent)        /* this paste affects the extent */
  306.     {
  307.         /* adjust range for extent */
  308.         /* write to driver */
  309.         this_ed = curr_ext.ed_level;
  310.             /* data setup here */
  311.         iac_code = iac_write_data((**the_data_H).doc_ID, 
  312.                                   curr_ext.hat_check, 
  313.                                   &this_ed,
  314.                                   1,
  315.                                   ext_data);
  316.  
  317.         curr_ext.ext_end += delta;        /* now update extent */
  318.         curr_ext.ed_level = this_ed;
  319.     }
  320. }
  321.  
  322.  
  323. /**
  324.  * Routine: kill_extent
  325.  *
  326.  *        This removes the current extent from the table and compresses the
  327.  *    table. The count and "active flag" are also updated.
  328.  */
  329.  
  330. # define __SEG__ Main
  331. void    kill_extent()    /* always removes current extent */
  332. {
  333. win_dataH        the_data_H;            /* data associated with a window */
  334. extentH            the_extH;            /* handle to extent block */
  335. exTable            ext_recs;
  336. long            the_doc;            /* local copies due to memory mashing */
  337. short            slot_ID, h_check;
  338. short            e_cnt, startt;
  339. short            i;
  340.  
  341. short            iac_code = noErr;    /* result from IAC call */
  342.  
  343. extern    Boolean        ext_active;
  344.  
  345.      the_data_H = (win_dataH) GetWRefCon (myWindow);
  346.     the_extH =     (**the_data_H).the_extents;
  347.     e_cnt =         (**the_data_H).ext_cnt;
  348.     startt =     curr_ext.ext_strt;
  349.  
  350.     ext_recs = *the_extH;        /* point to table base */
  351.     for (i=0; i<e_cnt; i++)
  352.     {
  353.         if (ext_recs[i].ext_strt == startt)    /* found current extent */
  354.         {
  355.             iac_code = iac_remove_dependency((**the_data_H).doc_ID,
  356.                                              (**the_data_H).the_slot, 
  357.                                              ext_recs[i].hat_check);
  358.             BlockMove(&ext_recs[i+1],    /* move tail down */
  359.                       &ext_recs[i],
  360.                       (e_cnt-i)*sizeof(extent));
  361.             (**the_data_H).ext_cnt -= 1;
  362.             (**the_data_H).dirty = true;
  363.             break;
  364.         }
  365.     }
  366.     ext_active = false;
  367. }